﻿using System.Net;
using System.Runtime.Serialization.Formatters;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : SingletonMono<GameManager>
{
    public GameObject gameObj;
    public Material[] material;
    E_Game_State game_State;
    Vector3 moveVec = new Vector3(0, 0, 0);
    List<BaseGrid> list = new List<BaseGrid>();
    public Vector3 MoveVec { get => moveVec; set => moveVec = value; }
    internal E_Game_State Game_State
    {
        get => game_State;
        set
        {
            game_State = value;
            StateGameStateLogic(Game_State);
        }
    }

    override protected void Awake()
    {
        base.Awake();
        InputMgr.GetInstance().StartOrEndCheck(true);
        MapContorller.GetInstance().DrawMap(24, 8, gameObj, material, Vector3.zero, this.transform);
        PlayerController.GetInstance().Init();
        MonitorInputEvent();
    }

    void Start()
    {

        CameraContorller.GetInstance().CreatCamera();
        EventService.AddEventListener<Vector3>(E_Event_Type.CreatObj, PropController.GetInstance().CreatProp);
        PropController.GetInstance().CreatPropGameObj();
        DrawMagic.GetInstance().Init();
        Game_State = E_Game_State.Idle;
    }
    void MonitorInputEvent() //监听按键事件
    {
        EventService.AddEventListener<KeyCode>(E_Event_Type.KeyDown, ChangeState);
    }
    void ChangeState(KeyCode key)
    {
        if (key == KeyCode.Space)
        {
            if (Game_State == E_Game_State.End)
            {
                Game_State = E_Game_State.Idle;
            }
            if (Game_State == E_Game_State.Idle)
            {
                Game_State = E_Game_State.Ready;
            }

        }
        if (key == KeyCode.O)
        {
            //Debug.Log("O");
            Game_State = E_Game_State.Play;
        }

    }
    void RealTimePointPos()
    {
        RaycastHit hit;
        Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 1000, 1 << LayerMask.NameToLayer("SceneObject") | 1 << LayerMask.NameToLayer("Default"), QueryTriggerInteraction.UseGlobal);
        if (hit.transform != null)
        {
            if (hit.transform.gameObject.tag == "floor")
            {
                DrawMagic.GetInstance().MousePos = hit.transform.position;
            }
        }
    }
    void StateGameStateLogic(E_Game_State state)
    {
        switch (state)
        {
            case E_Game_State.Idle:
                CameraContorller.GetInstance().Reset();
                EventService.EventTrigger(E_Event_Type.CreatPlayer);
                Game_State = E_Game_State.Ready;
                break;
            case E_Game_State.Ready:

                if (MagicController.GetInstance().MagicGraphs.Count > 0)
                {
                    for (int i = 0; i < MagicController.GetInstance().MagicGraphs.Count; i++)
                    {
                        for (int j = 0; j < MagicController.GetInstance().MagicGraphs[i].Magiclines.Count; j++)
                        {
                            MagicController.GetInstance().MagicGraphs[i].Magiclines[j].Line.gameObject.SetActive(true);
                        }
                    }
                }
                break;
            case E_Game_State.Play:
                MagicController.GetInstance().DeOnTest();
                EnemyController.GetInstance().CreatGameObject();
                break;
            case E_Game_State.End:
                PlayerController.GetInstance().Recycle();
                EnemyController.GetInstance().Recycle();
                MagicController.GetInstance().Recycle();
                PropController.GetInstance().Recycle();
                break;
            case E_Game_State.Restart:
                break;
        }
    }
    private void Update()
    {
        if (Input.GetKey(KeyCode.LeftShift) && Input.GetMouseButtonDown(0)) //攻击
        {
            RaycastHit hit;
            Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 1000, 1 << LayerMask.NameToLayer("SceneObject") | 1 << LayerMask.NameToLayer("Default"), QueryTriggerInteraction.UseGlobal);

            if (hit.transform != null)
            {
                if (hit.transform.gameObject.tag == "floor")
                {
                    EventService.EventTrigger<Vector3>(E_Event_Type.CreatObj, hit.point);
                }
            }

        }
        else if (Input.GetMouseButtonDown(0))//插旗
        {
            RaycastHit hit;
            Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 1000, 1 << LayerMask.NameToLayer("SceneObject") | 1 << LayerMask.NameToLayer("Default"), QueryTriggerInteraction.UseGlobal);
            if (hit.transform != null)
            {
                if (hit.transform.gameObject.tag == "floor")
                {
                    MapContorller.GetInstance().ArrayGrid[(int)hit.transform.position.z, (int)hit.transform.position.x].DebugAllLineInGrid();
                    //string[] point = hit.transform.gameObject.name.Split('*');
                    //list.Add(MapContorller.GetInstance().ArrayGrid[int.Parse(point[0]), int.Parse(point[1])]); //选中的方块加入到计算列表中
                    DrawMagic.GetInstance().ClickToDraw(hit.transform.position);
                }
            }
        }
        else if (Input.GetMouseButtonDown(1))//移动
        {
            RaycastHit hit;
            Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 1000, 1 << LayerMask.NameToLayer("SceneObject") | 1 << LayerMask.NameToLayer("Default"), QueryTriggerInteraction.UseGlobal);
            if (hit.transform != null)
            {
                if (hit.transform.gameObject.tag == "floor" && PlayerController.GetInstance().Player != null)
                {
                    EventService.EventTrigger<Vector3, Vector3, bool>(E_Event_Type.PlayerMove, hit.point - PlayerController.GetInstance().Player.Obj.transform.position, hit.point, true);
                }
            }
        }
        else if (Input.GetKey(KeyCode.LeftControl) && Input.GetKeyDown(KeyCode.Z))
        {
            EventService.EventTrigger(E_Event_Type.RevocationMagicLine);
        }
        RealTimePointPos();
        if (Game_State == E_Game_State.Ready)
        {
            if (Input.GetKeyDown(KeyCode.U))
            {

                EventService.EventTrigger<bool>(E_Event_Type.StartDrawMagicLine, true);
            }
            if (Input.GetKeyDown(KeyCode.I))
            {
                EventService.EventTrigger<bool>(E_Event_Type.EndDrawMagicLine, false);

            }
            if (Input.GetMouseButton(0))
            {

            }
            RaycastHit hit;
            Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 1000, 1 << LayerMask.NameToLayer("MagicGraph") | 1 << LayerMask.NameToLayer("Default"), QueryTriggerInteraction.UseGlobal);
            if (hit.transform != null)
            {
                if (hit.transform.gameObject.tag == "line")
                {
                    Debug.Log("111");
                }
            }
        }

    }

}
/// <summary>
///   0:准备|1：游玩|2：结算|3：重新开始
/// </summary>
enum E_Game_State
{
    Idle = 0,
    Ready = 1,
    Play = 2,
    Reap = 3,
    End = 4,
    Restart = 5,
}
/// <summary>
/// 0：布置|1：等待|2：打斗
/// </summary>
enum E_Play_State
{
    Layout = 0,
    Wait = 1,
    Fight = 2
}